package com.sdcqjy.property.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.sdcqjy.mylibrary.widget.ShowToast;
import com.sdcqjy.property.R;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.presenter.ChangePresenter;
import com.sdcqjy.property.presenter.contract.ChangeContract;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 描述：
 * Created by rookieCi on 2017/9/22.
 */
public class ChangeNameActivity extends SimpleBaseActivity<ChangePresenter> implements ChangeContract.View {
    @BindView(R.id.editName)
    EditText editName;
    @BindView(R.id.textTitle)
    TextView textTitle;
    private int idx;//0姓名，1邮箱

    public static void open(Context context, int idx) {
        Intent intent = new Intent(context, ChangeNameActivity.class);
        intent.putExtra("idx", idx);
        context.startActivity(intent);
    }

    public static void open(Context context) {
        open(context, 0);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_name);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        idx = intent.getIntExtra("idx", 0);
        presenter = new ChangePresenter(this);
        LoginMode mode = LoginMode.getMode(getActivity());
        if (idx == 1) {
            if (!TextUtils.isEmpty(mode.email)) editName.setText(mode.email);
            editName.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
            textTitle.setText(R.string.youxiang);
            editName.setHint(R.string.qsryx);
        } else {
            editName.setText(mode.userName);
        }
    }

    @OnClick(R.id.imgR)
    public void onViewClicked() {
        name = editName.getText().toString();
        if (!TextUtils.isEmpty(name)) {
            LoginMode mode = LoginMode.getMode(getActivity());
            if (idx == 1) {
                if (name.equals(mode.email)) {
                    onBackPressed();
                } else {
                    presenter.changeEmail(name);
                }
            } else {
                if (name.equals(mode.userName)) {
                    onBackPressed();
                } else {
                    presenter.changeName(name);
                }
            }
        } else {
            if (idx == 1) ShowToast.alertShortOfWhite(getActivity(), R.string.qsryx);
            else ShowToast.alertShortOfWhite(getActivity(), R.string.qsryhm);
        }
    }

    private String name;

    public void changeSucc() {
        LoginMode mode = LoginMode.getMode(getActivity());
        if (idx == 1) {
            mode.email = name;
        } else {
            mode.userName = name;
        }
        LoginMode.setMode(getActivity(), mode);
        onBackPressed();
    }
}
